using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace FactoryAutoMatic.Utils
{
  class BasicTool
  {
      public BasicTool() { }

    public string SetAppSettings(string type, string value)
      {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        if (type == "sensor")
        {
          config.AppSettings.Settings["SensorCOM"].Value = value;
        }
        else if (type == "scanner")
        {
          config.AppSettings.Settings["ScannerCOM"].Value = value;
        }
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
        return null;
      }

      //电文写入log
      public void WriteLog(string xmlMsg, string type, string messageClass)
      {
        try
        {
          XmlDocument doc = new XmlDocument();
          doc.LoadXml(xmlMsg);
          doc.Save("xml\\" + type + "\\Send_" + messageClass + ".xml");
        }
        catch (Exception ex)
        {
          throw ex;
        }
      }

      //执行sql语句
      public int Excutesql(string sql)
      {
        SqlConnection conn = GetConn();
        try
        {
          conn.Open();
          SqlCommand cmd = new SqlCommand(sql, conn);
          int count = cmd.ExecuteNonQuery();
          return count;
        }
        catch (Exception ex)
        {
          throw ex;
        }
        finally
        {
          conn.Close();
        }
      }
      public void Excutetest(DataTable dt, string sql)
      {
        SqlConnection conn = GetConn();
        try
        {
          conn.Open();
          SqlCommand cmd = new SqlCommand(sql, conn);
          SqlDataAdapter adapter = new SqlDataAdapter(cmd);
          adapter.Fill(dt);
          SqlCommandBuilder scb = new SqlCommandBuilder(adapter);

          if (dt.GetChanges() != null)
          {

            adapter.Update(dt.GetChanges());
            dt.AcceptChanges();
          }

        }
        catch (Exception ex)
        {

          throw ex;
        }
        finally
        {
          conn.Close();
        }

      }
       //数据库查询 19
      public DataTable ExcuteQuery_19(string sql)
      {
        DataTable dt = new DataTable();
        SqlConnection conn = GetConn19();
        try
        {
          conn.Open();
          SqlCommand cmd = new SqlCommand(sql, conn);
          SqlDataAdapter adapter = new SqlDataAdapter(cmd);
          adapter.Fill(dt);
          return dt;
        }
        catch (Exception ex)
        {

          throw ex;
        }
        finally
        {
          conn.Close();
        }
      }


      //数据库查询 15
      public DataTable ExcuteQuery(string sql)
      {
        DataTable dt = new DataTable();
        SqlConnection conn = GetConn();
        try
        {
          conn.Open();
          SqlCommand cmd = new SqlCommand(sql, conn);
          SqlDataAdapter adapter = new SqlDataAdapter(cmd);
          adapter.Fill(dt);
          return dt;
        }
        catch (Exception ex)
        {
          throw ex;
        }
        finally
        {
          conn.Close();
        }
      }

      public SqlConnection GetConn()
      {
        string connectionString = "Server=10.130.1.19;Initial Catalog=AKS2019;User ID=sa;Password=abcd1234!";
        //string connectionString = ConfigurationManager.ConnectionStrings["SMTDB"].ToString();
        SqlConnection conn = new SqlConnection(connectionString);
        return conn;
      }

      public SqlConnection GetConn19()
      {
        string connectionString = ConfigurationManager.ConnectionStrings["SMTDB19"].ToString();
        SqlConnection conn = new SqlConnection(connectionString);
        return conn;
      }
      //执行程序
      public DataSet ExcuteProcedure(string procname, params SqlParameter[] parameters)
      {
        SqlConnection conn = GetConn();
        DataSet ds = new DataSet();
        conn.Open();
        SqlTransaction st = conn.BeginTransaction();
        SqlCommand cmd = new SqlCommand();
        cmd.Transaction = st;
        cmd.Connection = conn;
        try
        {
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.CommandText = procname;
          cmd.Parameters.AddRange(parameters);
          SqlDataAdapter sda = new SqlDataAdapter();
          sda.SelectCommand = cmd;
          sda.Fill(ds);
          st.Commit();
        }
        catch (Exception ex)
        {
          st.Rollback();
          throw ex;
        }
        finally
        {
          conn.Close();
        }
        return ds;

      }

      public void WriteLog(string msg)
      {
        try
        {
          string path = Path.Combine("", "./log/" + DateTime.Now.ToString("yyyy-MM"));
          if (!Directory.Exists(path))//判断是否有该文件 
            Directory.CreateDirectory(path);
          string logFileName = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//生成日志文件 
          if (!File.Exists(logFileName))//判断日志文件是否为当天
          {
            FileStream fs;
            fs = File.Create(logFileName);//创建文件
            fs.Close();
          }
          StreamWriter writer = File.AppendText(logFileName);//文件中添加文件流

          writer.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "  " + msg + "\r\n");
          writer.WriteLine("-------------------------------------------------------------------");
          writer.Flush();
          writer.Close();

        }
        catch (Exception e)
        {
          throw e;
        }

      }

}
}
